package net.xiaosaguo.mymall.product.app;

import net.xiaosaguo.common.utils.PageUtils;
import net.xiaosaguo.common.utils.R;
import net.xiaosaguo.mymall.product.entity.AttrEntity;
import net.xiaosaguo.mymall.product.entity.AttrGroupEntity;
import net.xiaosaguo.mymall.product.service.AttrAttrgroupRelationService;
import net.xiaosaguo.mymall.product.service.AttrGroupService;
import net.xiaosaguo.mymall.product.service.AttrService;
import net.xiaosaguo.mymall.product.service.CategoryService;
import net.xiaosaguo.mymall.product.vo.AttrAttrGroupRelationVO;
import net.xiaosaguo.mymall.product.vo.AttrGroupWithAttrVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author xiaosaguo
 * @email guoyx@vip.qq.com
 * @date 2020-06-04 23:30:36
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * description: 根据属性分组获取属性分组关联的所有属性
     *
     * @param attrgroupId 属性分组 ID
     * @return 所有关联该属性分组的属性
     * @author xiaosaguo
     * @date 2020/06/13 10:28
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrByAttrGroup(@PathVariable Long attrgroupId) {
        List<AttrEntity> list = attrService.getAttrByAttrGroup(attrgroupId);

        return R.ok().put("data", list);
    }

    /**
     * description: 获取分类下所有分组&关联属性
     *
     * @param catelogId 商品分类 ID
     * @return 所有关联该属性分组的属性
     * @author xiaosaguo
     * @date 2020/6/15 19:56:46
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrByCategoryId(@PathVariable Long catelogId) {
        List<AttrGroupWithAttrVO> list = attrGroupService.getAttrGroupWithAttrByCategoryId(catelogId);
        return R.ok().put("data", list);
    }

    /**
     * description: 根据属性分组获取获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     *
     * @param attrgroupId 属性分组 ID
     * @return 分组里面还没有关联的本分类里面的其他基本属性
     * @author xiaosaguo
     * @date 2020/06/13 10:28
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getAttrByAttrGroupNoRelation(@RequestParam Map<String, Object> params, @PathVariable Long attrgroupId) {
        PageUtils page = attrService.getAttrByAttrGroupNoRelation(params, attrgroupId);

        return R.ok().put("page", page);
    }

    /**
     * description: 根据商品三级分类查询属性分组，默认 0 为查询全部
     *
     * @param params    分页参数
     * @param catelogId 商品三级分类 id
     * @return net.xiaosaguo.common.utils.PageUtils
     * @author xiaosaguo
     * @date 2020/06/13 09:34
     */
    @GetMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId) {

        /// PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrAttrGroupRelationVO> voList) {

        attrAttrgroupRelationService.saveBatch(voList);

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * description: 批量删除属性与分组的关联关系
     *
     * @param list AttrAttrGroupRelationVOList
     * @author xiaosaguo
     * @date 2020/06/13 12:25
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrAttrGroupRelation(@RequestBody List<AttrAttrGroupRelationVO> list) {
        attrService.deleteAttrAttrGroupRelation(list);

        return R.ok();
    }

}
